<template>
  <el-card shadow="always">
    <!--table表格:展示全部SPU下的全部SKU-->
    <el-table border stripe :data="skuArr">
      <el-table-column
        type="index"
        align="center"
        width="80"
        label="序号"
      ></el-table-column>
      <el-table-column
        label="名称"
        width="120"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        width="120"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="默认图片" width="120">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column label="重量" width="120" prop="weight">
      </el-table-column>
      <el-table-column label="价格" width="120" prop="price"></el-table-column>
      <el-table-column label="操作">
        <template #="{ row, $index }">
          <el-button
            :type="row.isSale ? 'primary' : 'info'"
            size="small"
            :icon="row.isSale ? Bottom : Top"
            @click="changeSale(row)"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            @click="showMessage"
          ></el-button>
          <el-button
            type="info"
            size="small"
            :icon="InfoFilled"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            @click="deleteSkuInfo(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin: 10px 0px"
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 7, 9]"
      :small="true"
      :disabled="false"
      :background="true"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getSkuList"
      @size-change="sizeHandler"
    />
  </el-card>
  <el-drawer v-model="drawer" title="SKU信息展示" :with-header="false">
    <!--栅格系统:24栏
        el-row:行
        el-col:列
      -->
    <el-row>
      <el-col :span="6">SKU名称</el-col>
      <el-col :span="18">{{ skuInfo.skuName }}</el-col>
    </el-row>
    <el-row>
      <el-col :span="6">SKU描述</el-col>
      <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
    </el-row>
    <el-row>
      <el-col :span="6">SKU价格</el-col>
      <el-col :span="18">{{ skuInfo.price }}</el-col>
    </el-row>
    <el-row>
      <el-col :span="6">平台属性</el-col>
      <el-col :span="18">
        <el-tag
          type="success"
          style="margin: 0px 5px"
          v-for="(item, index) in skuInfo.skuAttrValueList"
          :key="item.id"
          >{{ item.valueName }}</el-tag
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="6">销售属性</el-col>
      <el-col :span="18">
        <el-tag
          type="success"
          v-for="(item, index) in skuInfo.skuSaleAttrValueList"
          :key="item.id"
          >{{ item.saleAttrValueName }}</el-tag
        >
      </el-col>
    </el-row>

    <el-row style="margin: 40px">
      <el-col>
        <el-carousel :interval="4000" type="card" height="200px">
          <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
            <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%" />
          </el-carousel-item>
        </el-carousel>
      </el-col>
    </el-row>
  </el-drawer>
</template>

<script setup lang="ts">
import { Top, Bottom, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import {
  reqCancelSale,
  reqOnSale,
  reqRemoveSku,
  reqSkuInfo,
  reqSkuList,
} from "../../../api/product/sku/index";
//当前页码1
let currentPage = ref(1);
//一页展示的条数
let pageSize = ref(3);
//存储已有的SKU列表数据与总数
let skuArr = ref([]);
let total = ref(0);
//存储第三个按钮查看sku信息的数据
let skuInfo = ref<any>([]);
//展示抽屉的数据
let drawer = ref(false);
onMounted(() => {
  //组件挂载完毕获取一些数据
  getSkuList();
});

const getSkuList = async (pager = 1) => {
  //整理参数:当前页码
  currentPage.value = pager;
  //发请求获取前部SKU列表的数据
  let result = await reqSkuList(currentPage.value, pageSize.value);
  //存储数据：SKU
  skuArr.value = result.records;
  total.value = result.total;
};
const sizeHandler = (limit: number) => {
  pageSize.value = limit;
  getSkuList();
};
//改变商品的上架下架状态
const changeSale = async (row: any) => {
  if (row.isSale == 1) {
    await reqCancelSale(row.id);
    ElMessage.success("商品下架成功");
  } else {
    await reqOnSale(row.id);
    ElMessage.success("商品上架成功");
  }
  //再次获取更新后的全部SKU数据
  getSkuList();
};
//点击第二个按钮弹出提示框
const showMessage = () => {
  ElMessage.warning("正在开发中...");
};
//点击第三个按钮弹出抽屉展示信息
const getSkuInfo = async (sku: any) => {
  drawer.value = true;
  //根据已有的SKU的ID获取详情数据
  let result = await reqSkuInfo(sku.id);
  skuInfo.value = result;
};
//点击删除按钮删除当前sku
const deleteSkuInfo = async (row: any) => {
  try {
    await reqRemoveSku(row.id);
    ElMessage.success("删除成功");
    getSkuList(
      skuArr.value.length > 1 ? currentPage.value : currentPage.value - 1
    );
  } catch (e) {
    ElMessage.error("删除失败");
  }
};
</script>

<style scoped>
</style>